<?php
function send_post( $url , $post_data ) {

    $postdata = json_encode( $post_data );
    $options = array (
        'http' => array (
            'method' => 'POST' ,
            'header' => 'Content-type:application/json' ,
            'content' => $postdata ,
            'timeout' =>  60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create( $options );
    $res = file_get_contents ( $url , false, $context );
    return  $res;
}
//curl操作,获取返回值,为数组类型
function get_curl_json($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($ch);
    if(curl_errno($ch)){
        print_r(curl_error($ch));
    }
    curl_close($ch);
    return json_decode($result,TRUE);
}
function render($int , $msg){
    return json_encode(array($int,$msg ));
}

/**
* 异步发送一个请求
* @param string $url 请求的链接
* @param mixed $params 请求的参数
* @param string $method 请求的方法
* @return boolean TRUE
**/


// 定义回调函数，判断数组元素是否在另一个数组中
function filterArray($arr1, $arr2) {
    foreach ($arr1 as $key => $value) {
      if (in_array($value['tel'], array_column($arr2, 'tel'))) {
        unset($arr1[$key]);
      }
    }
    return $arr1;
  }
function send_request($url, $data = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_exec($ch);
    curl_close($ch);
}

function _fsockopen($url,$post_data=array(),$cookie=array()){
    $url_arr = parse_url($url);
    $port = isset($url_arr['port'])?$url_arr['port']:80;
    if($url_arr['scheme'] == 'https'){
        $url_arr['host'] = 'ssl://'.$url_arr['host'];
    }
    $fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);
    if(!$fp) return false;
    $getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';
    $getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';
    $method = 'GET';  //默认get方式
    if(!empty($post_data)) $method = 'POST';
    $header = "$method  $getPath  HTTP/1.1\r\n";
    $header .= "Host: ".$url_arr['host']."\r\n";
    if(!empty($cookie)){  //传递cookie信息
        $_cookie = strval(NULL);
        foreach($cookie AS $k=>$v){
            $_cookie .= $k."=".$v.";";
        }
        $cookie_str = "Cookie:".base64_encode($_cookie)."\r\n";
        $header .= $cookie_str;
    }
    if(!empty($post_data)){  //传递post数据
        $_post = array();
        foreach($post_data AS $_k=>$_v){
            $_post[] = $_k."=".urlencode($_v);
        }
        $_post = implode('&', $_post);
        $post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n";
        $post_str .= "Content-Length: ".strlen($_post)."\r\n";  //数据长度
        $post_str .= "Connection:Close\r\n\r\n";
        $post_str .= $_post;  //传递post数据
        $header .= $post_str;
    }else{
        $header .= "Connection:Close\r\n\r\n";
    }
    fwrite($fp, $header);
    usleep(1000); // 这一句也是关键，如果没有这延时，可能在nginx服务器上就无法执行成功
    fclose($fp);
    return true;
}

function asyncRequest($url, $param = array(), $method = 'get', $port = 80, $timeout = 30)
{
 
    $errno = 0;
    $errstr = '';
    $info = parse_url($url);
    if (isset($param) && !empty($param)) {
        $query = trim(http_build_query($param));//数组转url字符串形式
    } else {
        $query = null;
    }
    if (!isset($info['path']) || empty($info['path'])) {
        $info['path'] = "/index.html";
    }
    $fp = fsockopen($info['host'], $port, $errno, $errstr, $timeout);
    if (!$fp) {
        return "连接失败";
    }
    if ($errno || !$fp) {
        return $errstr;
    }
    stream_set_blocking($fp, 0);//非阻塞
    stream_set_timeout($fp, 1);//相应超时时间(s)
 
    //注意header拼接中的空格
 
    if ($method == 'post') {
        $header = "POST " . $info['path'] . " HTTP/1.1\r\n";
        $header .= "Host:" . $info['host'] . "\r\n";
        $header .= "Content-Length:" . strlen($query) . "\r\n";
        $header .= "Content-type:application/x-www-form-urlencoded\r\n";
        $header .= "connection:close\r\n\r\n";
        $header .= $query;
    } else {
        if (empty($query)) {
            $header = "GET " . $info['path'] . " HTTP/1.1\r\n";
        } else {
            $header = "GET " . $info['path'] . "?" . $query . " HTTP/1.0\r\n";
        }
        $header .= "Host:" . $info['host'] . "\r\n";
        $header .= "connection:close\r\n\r\n";
    }
    $result = @fputs($fp, $header);
    ignore_user_abort(true);
    set_time_limit(0);
    @fclose($fp);
    return $result;
}